﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QN_DiemSV.Forms
{
    public partial class frmKhoa : Form
    {
        public frmKhoa()
        {
            InitializeComponent();
        }
        bool them = false;
        bool sua = false;
        Classes.connect conn = new QN_DiemSV.Classes.connect();
        void VisionControl()
        {
            txtKhoa.Enabled = true;
            txtTenKhoa.Enabled = true;
            txtDiaChi.Enabled = true;
        }
        void LoadData()
        {
            DataTable dt = new DataTable();
            dt = conn.excuteSelect("select * from KHOA");
            dgrKhoa.DataSource = dt;
        }

        private void frmKhoa_Load(object sender, EventArgs e)
        {
            txtKhoa.Enabled = false;
            txtTenKhoa.Enabled = false;
            txtDiaChi.Enabled = false;
            LoadData();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            them = true;
            VisionControl();
            txtKhoa.Focus();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            sua = true;
            VisionControl();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (them == true)
            {
                conn.EXE_PROC_PARAMS("KhoaInsert", "@MaK", txtKhoa.Text, "@TenKhoa", txtTenKhoa.Text, "@DiaChi", txtDiaChi.Text);
            }
            if (sua == true)
            {
                conn.excuteNonquery("update KHOA set TenKhoa ='" + txtTenKhoa.Text + "', DiaChi='" + txtDiaChi.Text + "' where MaKhoa='" + txtKhoa.Text.Trim() + "'");
            }
            LoadData(); 
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            conn.excuteNonquery("delete from KHOA where MaKhoa='" + txtKhoa.Text + "'");
            LoadData();
        }

        private void dgrKhoa_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            txtKhoa.Text = dgrKhoa.Rows[index].Cells["MaKhoa"].Value.ToString();
            txtTenKhoa.Text = dgrKhoa.Rows[index].Cells["TenKhoa"].Value.ToString();
            txtDiaChi.Text = dgrKhoa.Rows[index].Cells["DiaChi"].Value.ToString();
        }
    }
}
